//
//  YKClientExchangeDetailViewController.m
//  YunKang
//
//  Created by leoking870 on 2017/10/15.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKClientExchangeDetailViewController.h"
#import "YKInputView.h"
#import "YKInputMultilineView.h"
#import "YKPopupView.h"
#import "YKProcessTrackingViewController.h"
#import "YKAuditSuggestViewController.h"
#import "YKTurnOverViewController.h"
#import "YKClientExchangeDetailModel.h"
#import "YKApproveBottomView.h"
#import "WorkFlowManager.h"

@interface YKClientExchangeDetailViewController ()
@property (nonatomic, strong) YKClientExchangeDetailModel *mYKClientExchangeDetailModel;
@property (nonatomic, strong) UIButton* repealOrderButton;
@property (nonatomic, strong) UIButton *repositionOrderButton;
@property (nonatomic, strong) UIButton *confirmButton;
@end

@implementation YKClientExchangeDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"客户兑换详情";
    
    [self tf_showStateView:kTFViewStateLoading];
    [TFHTTPMANAER POST:@"customerPointsExchange/searchCustomerPointsExchangeById.htm" parameters:@{@"id":self.m_id} success:^(id responseObject) {
        self.mYKClientExchangeDetailModel = [YKClientExchangeDetailModel yy_modelWithJSON:responseObject[@"customerPointsExchange"]];
        [self tf_showStateView:kTFViewStateNone];
        [self updateViews];
        
    } failure:^(id responseObject, NSError *error) {
        [self tf_showStateView:kTFViewStateNetError];
    }];
    
}

- (void)updateViews {
    // Do any additional setup after loading the view.
    YKInputView *requestNoView = [[YKInputView alloc]initWithTitle:@"申请单号" input:self.mYKClientExchangeDetailModel.APPLY_CODE];
    [self.view addSubview:requestNoView];
    
    YKInputView *requestDateView = [[YKInputView alloc]initWithTitle:@"申请时间" input:self.mYKClientExchangeDetailModel.APPLY_DATE];
    [self.view addSubview:requestDateView];
    
    YKInputView *requestClientNameView = [[YKInputView alloc]initWithTitle:@"客户名称" input:self.mYKClientExchangeDetailModel.C_NAME];
    [self.view addSubview:requestClientNameView];
    
    YKInputView *requestMemberView = [[YKInputView alloc]initWithTitle:@"申请会员" input:[NSString stringWithFormat:@"%@|%@",self.mYKClientExchangeDetailModel.APPLY_PERSON,self.mYKClientExchangeDetailModel.USER_NAME]];
    [self.view addSubview:requestMemberView];
    
    
    
    YKInputMultilineView *exchangeContentView = [[YKInputMultilineView alloc]initWithTitle:@"申请内容" input:self.mYKClientExchangeDetailModel.EXCHANGE_CONTENT];
    [self.view addSubview:exchangeContentView];
    
    YKInputView *scoreView = [[YKInputView alloc]initWithTitle:@"兑换积分" input:self.mYKClientExchangeDetailModel.EXCHANGE_POINT];
    [self.view addSubview:scoreView];
    
    YKInputView *scoreLeftView = [[YKInputView alloc]initWithTitle:@"积分余额" input:self.mYKClientExchangeDetailModel.ENTERP_POINTS];
    [self.view addSubview:scoreLeftView];
    
    [self.view.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view.mas_leading).offset(13);
        make.trailing.equalTo(self.view.mas_trailing).offset(-13);
    }];
    [self.view.subviews mas_alignAlongAxis:MASAxisTypeVertical fixedSpace:@8 leadSpace:11];
    
    for (YKInputView *view in self.view.subviews) {
        view.editable = NO;
    }
    
    UIButton* repealOrderButton = [[UIButton alloc]init];
    [self.view addSubview:repealOrderButton];
    UIButton *repositionOrderButton = [[UIButton alloc]init];

    UIButton *processTrackButton = [[UIButton alloc]init];
    [self.view addSubview:processTrackButton];

    [self.view addSubview:repositionOrderButton];

    [repealOrderButton setTitle:@"申请驳回" forState:UIControlStateNormal];
    [repositionOrderButton setTitle:@"提交审批" forState:UIControlStateNormal];//原订单转审
    [processTrackButton setTitle:@"流程跟踪" forState:UIControlStateNormal];

    
    

    [repealOrderButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    [repositionOrderButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    [processTrackButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];

    repealOrderButton.titleLabel.font = [UIFont systemFontOfSize:14];
    repositionOrderButton.titleLabel.font = [UIFont systemFontOfSize:14];
    processTrackButton.titleLabel.font = [UIFont systemFontOfSize:14];

    [processTrackButton addTarget:self action:@selector(processTrack:) forControlEvents:UIControlEventTouchUpInside];
    [repealOrderButton addTarget:self action:@selector(repealOrder:) forControlEvents:UIControlEventTouchUpInside];
    [repositionOrderButton addTarget:self action:@selector(repositionOrder:) forControlEvents:UIControlEventTouchUpInside];

    [@[repealOrderButton,processTrackButton,repositionOrderButton/*,confirmButton*/] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:1 leadSpacing:0 tailSpacing:0];
    [@[repealOrderButton,processTrackButton,repositionOrderButton/*,confirmButton*/] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view);
        make.height.mas_equalTo(44);
    }];
    
    [repositionOrderButton setBackgroundImage:TFSTYLE(buttonEnableBackgroundImage) forState:UIControlStateNormal];
    [processTrackButton setBackgroundImage:TFSTYLE(buttonEnableBackgroundImage) forState:UIControlStateNormal];
    
    [repealOrderButton setBackgroundImage:TFSTYLE(buttonEnableBackgroundImage) forState:UIControlStateNormal];//
    [repealOrderButton setBackgroundImage:TFSTYLE(buttonDisableBackgroundImage) forState:UIControlStateDisabled];//

    [repositionOrderButton setBackgroundImage:TFSTYLE(buttonEnableBackgroundImage) forState:UIControlStateNormal];//
    [repositionOrderButton setBackgroundImage:TFSTYLE(buttonDisableBackgroundImage) forState:UIControlStateDisabled];//

    [self.view layoutIfNeeded];

    self.repealOrderButton = repealOrderButton;
    self.repositionOrderButton = repositionOrderButton;
    
    
    if (![self.mYKClientExchangeDetailModel.APPLY_STATUS isEqualToString:@"0"]) {
        repealOrderButton.enabled = NO;
    }
    if ([self.mYKClientExchangeDetailModel.APPLY_STATUS isEqualToString:@"2"] || [self.mYKClientExchangeDetailModel.APPLY_STATUS isEqualToString:@"3"]||[self.mYKClientExchangeDetailModel.APPLY_STATUS isEqualToString:@"1"]) {
        repealOrderButton.enabled = NO;
        repositionOrderButton.enabled = NO;
    }
    if (self.approveStatus) {
        [YKApproveBottomView addSelfToView:self ID:self.m_id  approveStatus:self.approveStatus];
    }
}

- (void)processTrack:(id)sender {
    YKProcessTrackingViewController *vc = [YKProcessTrackingViewController new];
    vc.m_id = self.m_id;
    [YKPopupView showPopupViewWithViewController:vc];
}
/**
 订单驳回
 */
- (void)repealOrder:(id)sender {
    YKAuditSuggestViewController *vc = [YKAuditSuggestViewController new];
    vc.submitBlock = ^(NSString *text) {
        [TFHTTPMANAER POST:@"customerPointsExchange/edtCustomerPointsExchange.htm" parameters:@{@"id":self.mYKClientExchangeDetailModel.ID,@"apply_status":@"3",@"pointBalance":self.mYKClientExchangeDetailModel.ENTERP_POINTS,@"exchangePoint":self.mYKClientExchangeDetailModel.EXCHANGE_POINT, @"clientid":self.mYKClientExchangeDetailModel.CLIENTID,@"rejectReason":text} HUD:@"订单驳回中..." success:^(id responseObject) {
            [self showHint:@"订单驳回成功"];
            [self.navigationController popViewControllerAnimated:YES];
        } failure:^(id responseObject, NSError *error) {
            [self showHint:error.localizedDescription];
        }];
    };
    [YKPopupView showPopupViewWithViewController:vc];
}
/**
 订单转审
 */
- (void)repositionOrder:(id)sender {
    [WorkFlowManager repositionApproveWithBusinessID:self.m_id fromViewController:self completion:^(BOOL succcess) {
        if(succcess){
            self.repealOrderButton.enabled = NO;
            self.confirmButton.enabled = NO;
        }
    }];
}
- (void)confirm:(id)sender {
    [TFHTTPMANAER POST:@"customerPointsExchange/edtCustomerPointsExchange.htm"
            parameters:@{@"id":self.mYKClientExchangeDetailModel.ID,@"apply_status":@"2",@"pointBalance":self.mYKClientExchangeDetailModel.ENTERP_POINTS,@"exchangePoint":self.mYKClientExchangeDetailModel.EXCHANGE_POINT, @"clientid":self.mYKClientExchangeDetailModel.CLIENTID}
                   HUD:@"确认审批中..."
               success:^(id responseObject) {
                   [self showHint:@"确认审批成功"];
                   [self.navigationController popViewControllerAnimated:YES];
               }
               failure:^(id responseObject, NSError *error) {
                   [self showHint:error.localizedDescription];
               }];
}

@end
